<?php

namespace App\Http\Controllers;

use App\Master;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;

class MasterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $masters = Master::latest('created_at')->Paginate(8);
        return view('Master.master', compact('masters'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('Master.createMaster');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rules = [
            'master_name' => 'required',
            'master_intro' => 'required|min:2',
            'master_avatar' => 'required'
        ];
        $this->validate($request, $rules);
        $this->uploadAvatar($request);
        $masterAvatar = $request->get('master_name')
            . '.' . $request->file('master_avatar')
                ->getClientOriginalExtension();

        $master = new Master;
        $master->master_avatar = $masterAvatar;
        $master->master_name = $request->get('master_name');
        $master->master_intro = $request->get('master_intro');
        $master->save();

        session()->flash('success', '大师创建成功');

        return redirect()->route('master.index');

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $master = Master::findOrFail($id);
        return view('Master.editMaster',compact('master'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $master = Master::findOrFail($id);

        if ($request->file('master_avatar'))
        {
            $avatar = $master->master_avatar;
            if ($avatar != 'default.jpg') {
                Storage::disk('uploads')->exists($avatar);
                Storage::disk('uploads')->delete($avatar);
            }
            $this->uploadAvatar($request);
            $master->master_avatar = $request->get('master_name')
                . '.' . $request->file('master_avatar')
                    ->getClientOriginalExtension();
            $master->save();
        }

        $master->update([
            'master_name' => $request->get('master_name'),
            'master_intro' => $request->get('master_intro'),
        ]);
        $master->save();

        session()->flash('success', '更新成功');
        return redirect()->route('master.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $master = Master::find($id);
        $avatar = $master->master_avatar;
        if ($avatar != 'default.jpg') {
            Storage::disk('uploads')->exists($avatar);
            Storage::disk('uploads')->delete($avatar);
        }
        $master->delete();

        session()->flash('success', '删除成功');
        return redirect()->route('master.index');
    }

    /**
     * Upload Avatar to public_path
     * @param Request $request
     */
    public function uploadAvatar(Request $request)
    {
        if ($request->isMethod('post') OR $request->isMethod('patch')) {

            $avatar = $request->file('master_avatar');

            if ($avatar->isValid()) {

                $realPath = $avatar->getRealPath();
                $ext = $avatar->getClientOriginalExtension();
                $masterName = $request->get('master_name');
                $fileName = $request->get('master_name'). '.' .$ext;
//              Storage::disk('uploads')->put($fileName, file_get_contents($realPath));
                //clip avatar
                Image::make($avatar)->fit(300, 300)->save(public_path('/uploads/avatars/') . $fileName);

            }
        }
    }
}
